# author Jozef Štaffen <jozef.staffen@gmail.com>

source "backend/do.sh"

DIR=("$HOME" "$HOME")
POS=("0" "0")

EDITOR="nano"
READER="less"

TEMPLATE="evil_kitty"

ACTIVE=0
INACTIVE=1
HELP=1
HIDDENFILES=0

FILE=()

COUNT=()

function getActivePanelId()
{
	if [ "$ACTIVE" -eq "0" ]
	then
		PANELID="left";
	else
		PANELID="right";
	fi;
}

function setActualFileParams()
{
	local P
	P=$(do_list "${DIR[${ACTIVE}]}" $HIDDENFILES | sed -n "$[${POS[$ACTIVE]}+1]p" | cut -d' ' -f "1 2")
	TYPE=${P% *}
	FILE=${P#* }
	FILE="${FILE//':;'/ }"
}

function setCounts()
{
	COUNT[0]=$(do_list "${DIR[0]}" $HIDDENFILES | wc -l)
	COUNT[1]=$(do_list "${DIR[1]}" $HIDDENFILES | wc -l)
}

function visitDir()
{
	local D
	D="${DIR[$ACTIVE]}/$FILE"
	D="$(echo "$D" | sed s'/^\/\//\//g')"
	
	if [ ! -d "$D" ] || [ ! -r "$D" ]
	then
		return
	fi
	
	POS=(0 0)
	DIR[$ACTIVE]="$D"
}

function leaveDir()
{
	local D
	D="${DIR[$ACTIVE]}"
	D="${D%'/'*}"
	
	if [ "$D" == "" ]
	then
		D="/"
	fi
	
	if [ ! -r "$D" ]
	then
		return
	fi
	
	DIR[$ACTIVE]="$D"
	POS=(0 0)
}

function listDirs()
{
	cat <(do_list "${DIR[0]}" $HIDDENFILES) <(echo) <(do_list "${DIR[1]}" $HIDDENFILES)
}

function changeActivePanel()
{
	ACTIVE=$[($ACTIVE+1)%2]
	INACTIVE=$[($INACTIVE+1)%2]
}

function toogleHiddenFiles()
{
	HIDDENFILES=$[($HIDDENFILES+1)%2]
	POS=(0 0)
}

function toogleHelpPanel()
{
	HELP=$[($HELP+1)%2]
}

function setPosition()
{
	local I=$1
	if [ "${COUNT[$ACTIVE]}" -gt "$I" ] && [ "$[$I+1]" -gt "0" ]
	then
		POS[$ACTIVE]=$I
	fi
}

function changePosition()
{
	local I=$1
	if [ "${COUNT[$ACTIVE]}" -gt $[${POS[$ACTIVE]}+$I] ]
	then
		if [ "$[${POS[$ACTIVE]}+$I+1]" -gt "0" ]
		then
			POS[$ACTIVE]="$[${POS[$ACTIVE]}+$I]"
		else
			POS[$ACTIVE]="0"
		fi
	else
		if [ "${COUNT[$ACTIVE]}" -ne "0" ]
		then
			POS[$ACTIVE]="$[${COUNT[$ACTIVE]}-1]"
		fi
	fi
}

function runEditor()
{
	if [ -f "${DIR[$ACTIVE]}/$FILE" ]
	then
		$EDITOR "${DIR[$ACTIVE]}/$FILE"
	fi
}

function viewFile()
{
	if [ -f "${DIR[$ACTIVE]}/$FILE" ]
	then
		$READER "${DIR[$ACTIVE]}/$FILE"
	fi
}

function doAction()
{
	while true
	do
		unset K1 K2 K3
		read -s -N1
		K1="$REPLY"
		read -s -N2 -t 0.0001
		K2="$REPLY"
		read -s -N1 -t 0.0001
		K3="$REPLY"
		read -s -N1 -t 0.0001
		K4="$REPLY"
		key="$K1$K2$K3$K4"
	
		case "$key" in
			$'\x1b\x4f\x51')	# F2
				do_rename "${DIR[$ACTIVE]}" "$FILE"
			;;
			
			$'\x1b\x4f\x52')	# F3
				viewFile
			;;
			
			$'\x1b\x4f\x53')	# F4
				runEditor
			;;
			
			$'\x1b\x5b\x31\x35\x7e')	# F5
				do_copy "${DIR[$ACTIVE]}" "$FILE" "${DIR[$INACTIVE]}"
			;;
			
			$'\x1b\x5b\x31\x37\x7e')	# F6
				do_move "${DIR[$ACTIVE]}" "$FILE" "${DIR[$INACTIVE]}"
			;;
			
			$'\x1b\x5b\x31\x38\x7e')	# F7
				do_createDir "${DIR[$ACTIVE]}"
			;;
			
			$'\x1b\x5b\x31\x39\x7e')	# F8
				do_delete "${DIR[$ACTIVE]}" "$FILE"
			;;
			
			$'\x1b\x5b\x32\x30\x7e')	# F9
				echo "Refreshing..."
			;;
			
			$'\x1b\x5b\x33\x7e')	# Delete
				do_delete "${DIR[$ACTIVE]}" "$FILE"
			;;
		
			$'\x1b\x4f\x48')	# Home
				setPosition "0"
			;;
		
			$'\x1b\x5b\x31\x7e')	# Home_keypad
				setPosition "0"
			;;
		
			$'\x1b\x4f\x46')	# End
				setPosition "$[${COUNT[$ACTIVE]}-1]"
			;;
		
			$'\x1b\x5b\x34\x7e')	# End_keypad
				setPosition "$[${COUNT[$ACTIVE]}-1]"
			;;
	
			$'\x1b\x5b\x35\x7e')	# Page_Up
				changePosition "-10"
			;;
	
			$'\x1b\x5b\x36\x7e')	# Page_Down
				changePosition "+10"
			;;
	
			$'\x1b\x5b\x41')	# Up_arrow
				changePosition "-1"
			;;
	
			$'\x1b\x5b\x42')	# Down_arrow
				changePosition "+1"
			;;
	
			$'\x1b\x5b\x43')	# Right_arrow
				visitDir
			;;
	
			$'\x1b\x5b\x44')	# Left_arrow
				leaveDir
			;;
			
			$'\x7f')	# Backspace
				leaveDir
			;;
	
			$'\x09')	# Tab
				changeActivePanel
			;;
	
			$'\x0a')	# Enter
				visitDir
			;;
			
			$'\x08')	# Ctrl+h
				toogleHelpPanel
			;;
			
			$'\x01')	# Ctrl+a
				toogleHiddenFiles
			;;
			
			$'\x0e')	# Ctrl+n
				do_createFile "${DIR[$ACTIVE]}"
			;;
			
			$'\x1b')	# Escape
				reset
				echo "Bye bye"
				exit 0
			;;
			
			*)		# Any key
				continue
			;;
		esac
		
		break
	done
}

function main()
{
	if [ $1 ]
	then
		TEMPLATE=${1}
	fi

	while true
	do
		setCounts
		setActualFileParams
		
		getActivePanelId
		listDirs | "./frontend/$TEMPLATE.sh" $PANELID ${COUNT[0]} ${POS[0]} ${COUNT[1]} ${POS[1]} $HELP "${DIR[0]}" "${DIR[1]}"
		
		doAction
	done
}
